package com.pms.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.pms.entity.Contact;
import com.pms.util.DBHelp;
import com.pms.util.Page;
import com.pms.util.RowMapper;

public class ContactDao {
	private DBHelp<Contact> db=new DBHelp<Contact>();
	Contact contact=new Contact();
	
	public Long getCount(String projectId){
		String sql="select count(*) from t_contact where projectid=?";
		return (Long)db.executeQueryForFunction(sql,projectId);
	}
	
	
	
	public void save(Contact contact){
		String sql="INSERT INTO t_contact(contactid,contactname,company,tel,phone,email,address,mainweb,blog,createtime,projectid,`desc`) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql, contact.getContactId(),contact.getContactName(),contact.getCompany(),contact.getTel(),contact.getPhone(),contact.getEmail(),contact.getAddress(),contact.getMainWeb(),contact.getBlog(),contact.getCreateTime(),contact.getProjectId(),contact.getDesc());
		
	}
	public Page<Contact> FindAll(String projectId,Page<Contact> page){
		
		String sql="select contactid,contactname,company,tel,phone,email,address,mainweb,blog,createtime,projectid,`desc` from t_contact where projectid=? order by createtime desc limit ?,?";
		List<Contact> list=db.executeQueryForList(sql,new ContactRowMapper(),projectId,page.getOffSet(),page.getPageSize());
		page.setResult(list);
		return page;
	
	}
	
	
public List<Contact> FindAll(String projectId){
		
		String sql="select contactid,contactname,company,tel,phone,email,address,mainweb,blog,createtime,projectid,`desc` from t_contact where projectid=? ";
		return db.executeQueryForList(sql,new ContactRowMapper(),projectId);
		
	}
	
	
	
	
	public Contact FindContactById(String contactId){
		String sql="select contactid,contactname,company,tel,phone,email,address,mainweb,blog,createtime,projectid,`desc` from t_contact where contactid=?";
		return db.executeQueryForObject(sql, new ContactRowMapper(), contactId);
	}
	public boolean updateContact(Contact contact){
		String sql="UPDATE t_contact SET contactname=?,company=?,tel=?,phone=?,email=?,address=?,mainweb=?,blog=?,`desc`=? where contactid=? ";
		return db.executeSQL(sql,contact.getContactName(),contact.getCompany(),contact.getTel(),contact.getPhone(),contact.getEmail(),contact.getAddress(),contact.getMainWeb(),contact.getBlog(),contact.getDesc(),contact.getContactId());
	}
	
	public void delContact(String contactId){
		String sql="DELETE FROM t_contact WHERE contactid=?";
		db.executeSQL(sql,contactId);
		
	}
	
	public Contact findByName(String contactName){
		String sql="SELECT contactid ,contactname,company,tel,email,address,mainweb,blog,createtime,projectid,`desc` FROM t_contact WHERE contactname=?";
		return db.executeQueryForObject(sql, new ContactRowMapper(), contactName);
	}
	
	public class ContactRowMapper implements RowMapper<Contact>{
		public Contact mapRow(ResultSet rs) throws SQLException {
			Contact contact=new Contact();
			contact.setContactId(rs.getString("contactId"));
			contact.setContactName(rs.getString("contactName"));
			contact.setCompany(rs.getString("company"));
			contact.setTel(rs.getString("tel"));
			contact.setPhone(rs.getString("phone"));
			contact.setEmail(rs.getString("email"));
			contact.setAddress(rs.getString("address"));
			contact.setMainWeb(rs.getString("mainWeb"));
			//String url=rs.getString("blog").substring(rs.getString("blog").lastIndexOf("/"));
			
			contact.setBlog(rs.getString("blog"));
			
			
			contact.setCreateTime(rs.getString("createTime"));
			contact.setProjectId(rs.getString("projectId"));
			contact.setDesc(rs.getString("desc"));
			return contact;
		}
		
	}

	
	

	
	
}
